package cn.schoolwow.workflow.module.task.action.flow.common;

import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;
import cn.schoolwow.util.domain.query.instanceList.QueryInstanceList;
import cn.schoolwow.workflow.domain.instance.ManualWorkFlowAssigner;
import cn.schoolwow.workflow.domain.task.WorkFlowAssigner;
import cn.schoolwow.workflow.entity.WorkFlowInstance;
import cn.schoolwow.workflow.entity.WorkFlowNode;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.List;

public class SetNodeAssignerCommonFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        WorkFlowNode workFlowNode = flowContext.checkInstanceData(WorkFlowNode.class);
        WorkFlowInstance workFlowInstance = flowContext.checkInstanceData(WorkFlowInstance.class);

        JSONArray assignerArray = workFlowNode.getAssigner();
        List<WorkFlowAssigner> workFlowAssignerList = assignerArray.toJavaList(WorkFlowAssigner.class);
        flowContext.putCurrentFlowData("workFlowAssignerList", workFlowAssignerList);

        List<String> assignerList = new ArrayList<>();
        for(int i=0;i<workFlowAssignerList.size();i++){
            WorkFlowAssigner workFlowAssigner = workFlowAssignerList.get(i);
            switch (workFlowAssigner.assigneeType){
                case 0:{
                    assignerList.addAll(workFlowAssigner.assignerList);
                }break;
                case 1:{
                    //发起人自选
                    JSONArray manualWorkFlowAssignerArray = workFlowInstance.getMeta().getJSONArray("manualWorkFlowAssignerList");
                    if(null==manualWorkFlowAssignerArray||manualWorkFlowAssignerArray.isEmpty()){
                        throw new IllegalArgumentException("未设置发起人自选审批人列表!节点名称:"+workFlowNode.getName()+",流程实例名称:"+workFlowInstance.getName());
                    }
                    List<ManualWorkFlowAssigner> manualWorkFlowAssignerList = manualWorkFlowAssignerArray.toJavaList(ManualWorkFlowAssigner.class);
                    ManualWorkFlowAssigner manualWorkFlowAssigner = QueryInstanceList.newQuery(manualWorkFlowAssignerList)
                            .addQuery("nodeName", workFlowNode.getName())
                            .addQuery("assignerGroupIndex", i)
                            .execute()
                            .getOne();
                    if(null==manualWorkFlowAssigner){
                        throw new IllegalArgumentException("发起人自选审批人不存在!节点名称:"+workFlowNode.getName()+",审批人序号:"+i);
                    }
                    assignerList.addAll(manualWorkFlowAssigner.assignerList);
                }break;
                default:{
                    throw new IllegalArgumentException("不支持的分配人类型!当前值:"+workFlowAssigner.assigneeType+",允许值[0,1]");
                }
            }
        }
        List<String> replaceAssignerList = new ArrayList<>();
        for(String currentAssigner:assignerList){
            flowContext.startFlow(new ReplaceAssignerCommonFlow())
                    .putCurrentCompositeFlowData("assigner", currentAssigner)
                    .execute();
            String assigner = flowContext.checkData("assigner", String.class);
            replaceAssignerList.add(assigner);
        }
        flowContext.putTemporaryData("replaceAssignerList", replaceAssignerList);
    }

    @Override
    public String name() {
        return "设置工作流节点实际审批人列表";
    }

}